import {
  api,
  util
} from '../../../js/util';
import {
  router
} from '../../../js/router';
import {
  cashier
} from '../../../js/modules/pay';
import {
  getRouterParams
} from '../../../js/modules/theme.js'
import authorize from '../../../js/modules/authorize.js';
import BasePage from '../../../js/common/base-page'
import { setStorageSync } from '../../../js/compatible';
const app = getApp();
BasePage({
  pageKey:"paySuccess",
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    mainColor:api.titleColor||"#e56b14",//主题色
    PayToken: '',
    reData: [],
    payName: "",
    isLoadComplete:false,
    cashier:true,
    Currency:'',//货币符号
    showLuckDraw:false//是否显示抽奖按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    this.luckdraw = this.selectComponent("#luckdraw");
    that.validateLogin = options.cashier == 'alone'?false:true;
    if (options.paytoken.length < 1) {
      util.alert("请求支付的数据不正确!请重新发起请求.");
      router.navigateBack();
    }
    this.setData({
      PayToken: options.paytoken,
      cashier:options.cashier == 'alone'?false:true
    });
    this.initOrderData();
  },
  /**初始化收银台订单数据 */
  initOrderData() {
    var that = this;
    cashier.loadCashierOrderData(that.data.PayToken, function(reData) {
      that.dealPayWay(reData);
      let showLuckDraw = false
      let entity = authorize.getEntity()
      if (reData.OrderDrawUrl != undefined && reData.OrderDrawUrl.length > 0) {
        showLuckDraw = true
      }
      // 未登录时，不显示抽奖按钮
      if (!entity) {
        showLuckDraw = false
      }
      that.setData({
        isLoadComplete:true,
        reData: reData,
        showLuckDraw
      });
    });

  },
  dealPayWay: function(data) {
    var name = "";
    if (data.PayName&&data.PayName!='--') {
      name = data.PayName;
    } else if (data.PayTypeId == 1) {
      name = '货到付款';
    } else if (data.PayTypeId == 2) {
      name = '在线支付';
    } else if (data.PayTypeId == 3) {
      name = '授信支付';
    } else if (data.PayTypeId == 4) {
      name = '转账汇款';
    } else if (data.PayTypeId == 5) {
      name = '线下付款';
    }
    this.setData({
      payName: name
    });
  },
  /**跳转到我的管理中心 */
  gotomyinfo: function() {
    setStorageSync('tabindex', app.dns.centerNum)
    router.switchTab({
      path: "center"
    });
  },
  //返回主页
  gotoIndex: function() {
    setStorageSync('tabindex', -1)
    router.switchTab({
      path: "index"
    });
  },
  //打开首页
  gotoHome: function() {
    router.navigateBack();
  },
  //参加抽奖
  onLuckDraw: function(e) {
    var url = e.currentTarget.dataset.url;
    let ps = url.split('?');
    let param = "";
    if (ps.length > 1) {
      param = ps[1];
    }
    let params = getRouterParams(param);
    var entity = authorize.getEntity() || {};
    params.IsOrderDraw = 1;
    params.StoreId = entity.fkId;
    this.luckdraw.showLuckDrawModal(params);
    //抽奖按钮只显示一次，弹出抽奖页面后就不再显示抽奖按钮
    this.setData({showLuckDraw: false});
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.navigateBack({//返回
      delta: 3
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})